package com.system.server;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class UserInter {

    VBox finalBox=new VBox();
    private final ObservableList<User>data= FXCollections.observableArrayList();

    public VBox creatUserUIPane(Object[] PaneList, Stage primaryStage)
    {
        GridPane pane=new GridPane();
        pane.setAlignment(Pos.BASELINE_CENTER);
        pane.setPadding(new Insets(50,50,50,50));
        pane.setHgap(10);
        pane.setVgap(10);


        Button cancelBtn=new Button("返回");
        Button selectOneBtn=new Button("查询用户");
        Button addBtn=new Button("添加用户");
        Label label=new Label("用户名：");
        TextField userName=new TextField();

        Label userText=new Label("用户查询");
        userText.setFont(Font.font("用户查询", FontWeight.BOLD,20));
        Label text=new Label("Tips:请输入用户名查询某用户或直接查询所有用户信息");

        Label tableName=new Label("用户信息：");
        TableView<User> resultArea=new TableView<>();
        TableColumn<User,Integer> idCol=new TableColumn<>("ID");
        idCol.setMaxWidth(50);
        idCol.setCellValueFactory(new PropertyValueFactory<>("user_id"));
        TableColumn<User,String> nameCol=new TableColumn<>("用户名");
        nameCol.setMinWidth(50);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("user_name"));
        TableColumn<User,String> pwdCol=new TableColumn<>("用户密码");
        pwdCol.setMinWidth(50);
        pwdCol.setCellValueFactory(new PropertyValueFactory<>("user_password"));
        TableColumn<User,String> nickCol=new TableColumn<>("用户昵称");
        nickCol.setMinWidth(50);
        nickCol.setCellValueFactory(new PropertyValueFactory<>("user_nickName"));
        TableColumn<User,String> emailCol=new TableColumn<>("email");
        emailCol.setMinWidth(100);
        emailCol.setCellValueFactory(new PropertyValueFactory<>("user_email"));
        TableColumn<User,String> addressCol=new TableColumn<>("用户地址");
        addressCol.setMinWidth(100);
        addressCol.setCellValueFactory(new PropertyValueFactory<>("user_address"));
        TableColumn<User,Double> moneyCol=new TableColumn<>("用户余额");
        moneyCol.setMinWidth(50);
        moneyCol.setCellValueFactory(new PropertyValueFactory<>("user_money"));

        resultArea.setItems(data);
        resultArea.getColumns().addAll(idCol,nameCol,pwdCol,nickCol,emailCol,addressCol,moneyCol);


        VBox vBox=new VBox();
        vBox.getChildren().add(tableName);
        vBox.getChildren().add(resultArea);

        HBox hBox=new HBox();
        hBox.setSpacing(10);
        hBox.getChildren().add(label);
        hBox.getChildren().add(userName);
        hBox.getChildren().add(selectOneBtn);
        hBox.getChildren().add(addBtn);

        pane.add(userText,0,0);
        pane.add(text,0,1);
        pane.add(hBox,0,2);
        pane.add(vBox,0,3);
        pane.add(cancelBtn,0,4);

        selectOneBtn.setOnAction(actionEvent -> {
            String name=userName.getText();
            if(!Objects.equals(name, "")){
                ResultSet res=UserDAO.selectWithName(name);
                try{
                    data.clear();
                    while (res.next()){
                        User user=new User();
                        user.setUser_id(res.getInt("user_id"));
                        user.setUser_name(name);
                        user.setUser_password(res.getString("user_password"));
                        user.setUser_nickName(res.getString("user_nickName"));
                        user.setUser_email(res.getString("user_email"));
                        user.setUser_address(res.getString("user_address"));
                        user.setUser_money(res.getDouble("user_money"));
                        data.add(user);
                        userName.clear();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else{
                ResultSet res=UserDAO.selectAllUser();
                try{
                    data.clear();
                    while(res.next()){
                        User user=new User();
                        user.setUser_id(res.getInt("user_id"));
                        user.setUser_name(res.getString("user_name"));
                        user.setUser_password(res.getString("user_password"));
                        user.setUser_nickName(res.getString("user_nickName"));
                        user.setUser_email(res.getString("user_email"));
                        user.setUser_address(res.getString("user_address"));
                        user.setUser_money(res.getDouble("user_money"));
                        data.add(user);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        addBtn.setOnAction(actionEvent -> {
            UserUI ui=new UserUI();
            ui.start(new Stage());
        });

        cancelBtn.setOnAction(actionEvent -> {
            finalBox.setVisible(false);
            ((VBox)PaneList[0]).setVisible(true);

            primaryStage.setWidth(300);
            primaryStage.setHeight(300);
            primaryStage.setTitle("管理员主界面");
        });

        finalBox.setAlignment(Pos.CENTER);
        finalBox.getChildren().addAll(pane);
        finalBox.setVisible(false);

        return finalBox;

    }

}
